package com.yuanyu.common.util;

import cn.hutool.core.util.StrUtil;
import com.yuanyu.common.constant.ApiConstant;
import com.yuanyu.common.exception.ParameterException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 断言工具类
 */
public class AssertUtil {
    public static void isNotEmpty(String str, String... message) {
        if (StrUtil.isBlank(str)) {
            execute(message);
        }
    }

    public static void notNull(Object obj, String... message) {
        if (obj == null) {
            execute(message);
        }
    }

    public static void isNull(Object obj, String... message) {
        if (obj != null) {
            execute(message);
        }
    }

    public static void notTrue(boolean b, String... message) {
        if (b) {
            execute(message);
        }
    }

    public static void notFalse(boolean b, String... message) {
        if (!b) {
            execute(message);
        }
    }

    public static void isTrue(boolean b, String... message) {
        if (!b) {
            execute(message);
        }
    }

    public static void isFalse(boolean b, String... message) {
        if (b) {
            execute(message);
        }
    }

    /**
     * 正则表达式判断内容工具
     *
     * @param pattern 正则表达式
     * @param str     判断内容
     * @param message 错误信息
     */
    public static void RegularExpression(String pattern, String str, String... message) {
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        if (!m.matches()) {
            execute(message);
        }
    }


    private static void execute(String... message) {
        String msg = ApiConstant.ERROR_MESSAGE;
        if (message != null && message.length > 0) {
            msg = message[0];
        }
        throw new ParameterException(msg);
    }
}
